//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Redemption.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Cashflows/SimpleCashFlow.h>
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CRedemption::CRedemption (Double amount, DateTime date) : CSimpleCashFlow(CRedemption::typeid)
{
    try
    {
#ifdef HANDLE
        _phRedemption = NULL;
#endif
        QuantLib::Real _amount = (QuantLib::Real)ValueHelper::Convert (amount); //d
        QuantLib::Date _date = (QuantLib::Date)ValueHelper::Convert (date); //d
        _ppRedemption = new boost::shared_ptr<QuantLib::Redemption> (new QuantLib::Redemption ( _amount,  _date ));
        SetSimpleCashFlow (boost::dynamic_pointer_cast<QuantLib::SimpleCashFlow> (*_ppRedemption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::CRedemption::CRedemption (boost::shared_ptr<QuantLib::Redemption>& childNative, Object^ owner) : CSimpleCashFlow(CRedemption::typeid)
{
#ifdef HANDLE
	_phRedemption = NULL;
#endif
	_ppRedemption = &childNative;
    _ppSimpleCashFlow = new boost::shared_ptr<QuantLib::SimpleCashFlow> (boost::dynamic_pointer_cast<QuantLib::SimpleCashFlow> (*_ppRedemption));
}
Cephei::QL::Cashflows::CRedemption::CRedemption (QuantLib::Redemption& childNative, Object^ owner) : CSimpleCashFlow(CRedemption::typeid)
{
#ifdef HANDLE
	_phRedemption = NULL;
#endif
	_ppRedemption = new boost::shared_ptr<QuantLib::Redemption> (&childNative);
    _ppSimpleCashFlow = new boost::shared_ptr<QuantLib::SimpleCashFlow> (boost::dynamic_pointer_cast<QuantLib::SimpleCashFlow> (*_ppRedemption));
    _RedemptionOwner = owner;
    _SimpleCashFlowOwner = owner;
}

Cephei::QL::Cashflows::CRedemption::CRedemption (CRedemption^ copy) : CSimpleCashFlow(CRedemption::typeid)
{
#ifdef HANDLE
	_phRedemption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppRedemption = new boost::shared_ptr<QuantLib::Redemption> (copy->GetShared());
        _ppSimpleCashFlow = new boost::shared_ptr<QuantLib::SimpleCashFlow> (boost::dynamic_pointer_cast<QuantLib::SimpleCashFlow> (*_ppRedemption));
    }
}
Cephei::QL::Cashflows::CRedemption::CRedemption (PLATFORM::Type^ t) : CSimpleCashFlow(CRedemption::typeid)
{
#ifdef HANDLE
	_phRedemption = NULL;
#endif
	if (!t->IsSubclassOf(CRedemption::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CRedemption::CRedemption (QuantLib::Handle<QuantLib::Redemption>& childNative, Object^ owner)  : CSimpleCashFlow(CRedemption::typeid)
{
	_phRedemption = &childNative;
	_ppRedemption = &static_cast<boost::shared_ptr<QuantLib::Redemption>>(childNative.currentLink());
    _ppSimpleCashFlow = new boost::shared_ptr<QuantLib::SimpleCashFlow> (boost::dynamic_pointer_cast<QuantLib::SimpleCashFlow> (*_ppRedemption));
    _RedemptionOwner = owner;
}
Cephei::QL::Cashflows::CRedemption::CRedemption (QuantLib::Handle<QuantLib::Redemption> childNative)  : CSimpleCashFlow(CRedemption::typeid)
{
	_phRedemption = &childNative;
	_ppRedemption = &static_cast<boost::shared_ptr<QuantLib::Redemption>>(childNative.currentLink());
    _ppSimpleCashFlow = new boost::shared_ptr<QuantLib::SimpleCashFlow> (boost::dynamic_pointer_cast<QuantLib::SimpleCashFlow> (*_ppRedemption));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CRedemption::CRedemption (QuantLib::Redemption childNative)  : CSimpleCashFlow(CRedemption::typeid)
{
#ifdef HANDLE
	_phRedemption = NULL;
#endif
	_ppRedemption = new boost::shared_ptr<QuantLib::Redemption> (new QuantLib::Redemption (childNative));
    _ppSimpleCashFlow = new boost::shared_ptr<QuantLib::SimpleCashFlow> (boost::dynamic_pointer_cast<QuantLib::SimpleCashFlow> (*_ppRedemption));
}
#endif

Cephei::QL::Cashflows::CRedemption::~CRedemption ()
{
    if (_ppRedemption != NULL)
    {
	    delete _ppRedemption;
        _ppRedemption = NULL;
    }
}
Cephei::QL::Cashflows::CRedemption::!CRedemption ()
{
    if (_ppRedemption != NULL)
    {
	    delete _ppRedemption;
    }
}
QuantLib::Redemption& Cephei::QL::Cashflows::CRedemption::GetReference ()
{
    if (_ppRedemption == NULL) throw REFNEW NativeNullException ();
	return **_ppRedemption;
}
boost::shared_ptr<QuantLib::Redemption>& Cephei::QL::Cashflows::CRedemption::GetShared ()
{
    if (_ppRedemption == NULL) throw REFNEW NativeNullException ();
	return *_ppRedemption;
}
QuantLib::Redemption* Cephei::QL::Cashflows::CRedemption::GetPointer ()
{
    if (_ppRedemption == NULL) throw REFNEW NativeNullException ();
	return &**_ppRedemption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Redemption>& Cephei::QL::Cashflows::CRedemption::GetHandle ()
{
	if (_phRedemption == NULL)
	{
		_phRedemption = new Handle<QuantLib::Redemption> (*_ppRedemption);
	}
	return *_phRedemption;
}
#endif
bool Cephei::QL::Cashflows::CRedemption::HasNative () 
{
	return (_ppRedemption != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::IRedemption^ Cephei::QL::Cashflows::CRedemption_Factory::Create (Double amount, DateTime date)
{
    return REFNEW CRedemption ( amount,  date);
}
